# Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

cc_library(
    name = "align",
    hdrs = ["align.h"],
    visibility = ["//visibility:public"],
)

cc_library(
    name = "logging",
    srcs = ["logging.cc"],
    hdrs = ["logging.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":chrono",
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "logging_test",
    srcs = ["logging_test.cc"],
    deps = [
        ":logging",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "logging_benchmark",
    srcs = ["logging_benchmark.cc"],
    deps = [
        ":logging",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "hazptr",
    srcs = [
        "hazptr/entry.h",
        "hazptr/entry_cache.cc",
        "hazptr/entry_cache.h",
        "hazptr/hazptr.h",
        "hazptr/hazptr_domain.cc",
        "hazptr/hazptr_domain.h",
        "hazptr/hazptr_object.cc",
        "hazptr/hazptr_object.h",
    ],
    hdrs = ["hazptr.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":never_destroyed",
        "//flare/base/internal:annotation",
        "//flare/base/internal:logging",
        "//flare/base/internal:memory_barrier",
        "//flare/base/internal:time_keeper",
    ],
)

cc_test(
    name = "hazptr_test",
    srcs = ["hazptr_test.cc"],
    deps = [
        ":chrono",
        ":hazptr",
        ":random",
        ":string",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "hazptr_benchmark",
    srcs = ["hazptr_benchmark.cc"],
    deps = [
        ":hazptr",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "tsc",
    srcs = ["tsc.cc"],
    hdrs = ["tsc.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":chrono",
        "//flare/base/internal:annotation",
        "//flare/base/internal:logging",
        "@com_github_google_glog//:glog",
    ],
)

cc_test(
    name = "tsc_test",
    srcs = ["tsc_test.cc"],
    deps = [
        ":logging",
        ":tsc",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "tsc_benchmark",
    srcs = ["tsc_benchmark.cc"],
    deps = [
        ":tsc",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "chrono",
    srcs = ["chrono.cc"],
    hdrs = ["chrono.h"],
    linkopts = [
        "-lpthread",
        "-lrt",
    ],
    visibility = ["//visibility:public"],
    deps = [":align"],
)

cc_test(
    name = "chrono_test",
    srcs = ["chrono_test.cc"],
    tags = ["exclusive"],
    deps = [
        ":chrono",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "chrono_benchmark",
    srcs = ["chrono_benchmark.cc"],
    deps = [
        ":chrono",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "string",
    srcs = ["string.cc"],
    hdrs = ["string.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
        "@com_github_fmtlib_fmt//:fmt",
    ],
)

cc_test(
    name = "string_test",
    srcs = ["string_test.cc"],
    deps = [
        ":random",
        ":string",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "buffer",
    srcs = ["buffer.cc"],
    hdrs = ["buffer.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":object_pool",
        ":ref_ptr",
        "//flare/base/buffer:builtin_buffer_block",
        "//flare/base/buffer:polymorphic_buffer",
        "//flare/base/internal:logging",
        "//flare/base/internal:singly_linked_list",
    ],
)

[
    cc_test(
        name = "buffer_test_" + block_size,
        srcs = ["buffer_test.cc"],
        local_defines = ['BUFFER_BLOCK_SIZE=\\"%s\\"' % block_size],
        deps = [
            ":buffer",
            "//flare/init:override_flag",
            "//flare/testing:main",
        ],
    )
    for block_size in [
        "4K",
        "64K",
        "1M",
    ]
]

cc_library(
    name = "demangle",
    srcs = ["demangle.cc"],
    hdrs = ["demangle.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":deferred",
    ],
)

cc_test(
    name = "demangle_test",
    srcs = ["demangle_test.cc"],
    deps = [
        ":demangle",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "object_pool",
    srcs = ["object_pool.cc"],
    hdrs = ["object_pool.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":likely",
        ":logging",
        "//flare/base/object_pool:disabled",
        "//flare/base/object_pool:global",
        "//flare/base/object_pool:memory_node_shared",
        "//flare/base/object_pool:thread_local",
    ],
)

cc_library(
    name = "down_cast",
    hdrs = ["down_cast.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "down_cast_test",
    srcs = ["down_cast_test.cc"],
    deps = [
        ":down_cast",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "maybe_owning",
    hdrs = ["maybe_owning.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
        "//flare/base/internal:test_prod",
    ],
)

cc_library(
    name = "never_destroyed",
    hdrs = ["never_destroyed.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "never_destroyed_test",
    srcs = ["never_destroyed_test.cc"],
    deps = [
        ":never_destroyed",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "delayed_init",
    hdrs = ["delayed_init.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "delayed_init_test",
    srcs = ["delayed_init_test.cc"],
    deps = [
        ":delayed_init",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "deferred",
    hdrs = ["deferred.h"],
    visibility = ["//visibility:public"],
    deps = [":function"],
)

cc_test(
    name = "deferred_test",
    srcs = ["deferred_test.cc"],
    deps = [
        ":deferred",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "maybe_owning_test",
    srcs = ["maybe_owning_test.cc"],
    visibility = ["//visibility:public"],
    deps = [
        ":maybe_owning",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "enum",
    hdrs = ["enum.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "enum_test",
    srcs = ["enum_test.cc"],
    deps = [
        ":enum",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "ref_ptr",
    hdrs = ["ref_ptr.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "ref_ptr_test",
    srcs = ["ref_ptr_test.cc"],
    deps = [
        ":chrono",
        ":random",
        ":ref_ptr",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "erased_ptr",
    srcs = ["erased_ptr.cc"],
    hdrs = ["erased_ptr.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":likely",
    ],
)

cc_test(
    name = "erased_ptr_test",
    srcs = ["erased_ptr_test.cc"],
    deps = [
        ":erased_ptr",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "endian",
    hdrs = ["endian.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "endian_test",
    srcs = ["endian_test.cc"],
    deps = [
        ":endian",
        ":logging",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "exposed_var",
    srcs = ["exposed_var.cc"],
    hdrs = ["exposed_var.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":deferred",
        ":string",
        ":tsc",
        ":write_mostly",
        "//flare/base/internal:logging",
        "//flare/base/internal:meta",
        "@com_github_jsoncpp//:jsoncpp",
    ],
)

cc_test(
    name = "exposed_var_test",
    srcs = ["exposed_var_test.cc"],
    deps = [
        ":exposed_var",
        "//flare/testing:main",
        "@com_github_jsoncpp//:jsoncpp",
    ],
)

cc_library(
    name = "status",
    srcs = ["status.cc"],
    hdrs = ["status.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":ref_ptr",
        "//flare/base/internal:early_init",
        "//flare/base/internal:logging",
        "//flare/base/internal:meta",
    ],
)

cc_test(
    name = "status_test",
    srcs = ["status_test.cc"],
    deps = [
        ":status",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "casting",
    hdrs = ["casting.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":demangle",
        "//flare/base/internal:index_alloc",
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "casting_test",
    srcs = ["casting_test.cc"],
    deps = [
        ":casting",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "casting_benchmark",
    srcs = ["casting_benchmark.cc"],
    deps = [
        ":casting",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "write_mostly",
    hdrs = ["write_mostly.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/write_mostly",
        "//flare/base/write_mostly:basic_ops",
        "//flare/base/write_mostly:metrics",
    ],
)

cc_library(
    name = "id_alloc",
    hdrs = ["id_alloc.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:annotation",
    ],
)

cc_test(
    name = "id_alloc_test",
    srcs = ["id_alloc_test.cc"],
    deps = [
        ":chrono",
        ":id_alloc",
        "//flare/base/thread:latch",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "id_alloc_benchmark",
    srcs = ["id_alloc_benchmark.cc"],
    deps = [
        ":id_alloc",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "function",
    hdrs = ["function.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":align",
        ":likely",
    ],
)

cc_test(
    name = "function_test",
    srcs = ["function_test.cc"],
    deps = [
        ":function",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "function_benchmark",
    srcs = ["function_benchmark.cc"],
    deps = [
        ":function",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "function_view",
    hdrs = ["function_view.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "function_view_test",
    srcs = ["function_view_test.cc"],
    deps = [
        ":function_view",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "future",
    hdrs = ["future.h"],
    visibility = ["//visibility:public"],
    deps = ["//flare/base/future"],
)

cc_library(
    name = "type_index",
    hdrs = ["type_index.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "type_index_test",
    srcs = ["type_index_test.cc"],
    deps = [
        ":type_index",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "random",
    hdrs = ["random.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:annotation",
    ],
)

cc_library(
    name = "callback",
    hdrs = ["callback.h"],
    visibility = ["//visibility:public"],
    deps = [
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "overloaded",
    hdrs = ["overloaded.h"],
    visibility = ["//visibility:public"],
)

cc_test(
    name = "overloaded_test",
    srcs = ["overloaded_test.cc"],
    deps = [
        ":overloaded",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "option",
    srcs = [
        "option.cc",
        "//flare/base/option:dynamically_changed.h",
        "//flare/base/option:gflags_provider.cc",
        "//flare/base/option:option_impl.h",
        "//flare/base/option:option_provider.cc",
        "//flare/base/option:option_provider.h",
        "//flare/base/option:option_service.cc",
        "//flare/base/option:option_service.h",
    ],
    hdrs = ["option.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":dependency_registry",
        ":expected",
        ":function",
        ":logging",
        ":never_destroyed",
        ":string",
        "//flare/base:status",
        "//flare/base/internal:time_keeper",
        "//flare/base/option:key",
        "//flare/base/thread:thread_cached",
        "//flare/init:on_init",
        "@com_github_gflags_gflags//:gflags",
        "@com_github_jsoncpp//:jsoncpp",
    ],
    alwayslink = True,
)

cc_test(
    name = "option_test",
    srcs = ["option_test.cc"],
    # Rainbow's SDK does not exit cleanly.
    # heap_check = "",
    deps = [
        ":option",
        "//flare:init",
        "//flare/base/option:json_parser",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "option_benchmark",
    srcs = ["option_benchmark.cc"],
    deps = [
        ":option",
        "@com_github_gflags_gflags//:gflags",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "monitoring",
    srcs = ["monitoring.cc"],
    hdrs = ["monitoring.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":chrono",
        ":deferred",
        ":exposed_var",
        ":logging",
        ":string",
        "//flare/base/internal:background_task_host",
        "//flare/base/internal:circular_buffer",
        "//flare/base/internal:cpu",
        "//flare/base/internal:dpc",
        "//flare/base/internal:hash_map",
        "//flare/base/internal:logging",
        "//flare/base/internal:time_keeper",
        "//flare/base/monitoring:dispatcher",
        "//flare/base/monitoring:event",
        "//flare/base/monitoring:fwd",
        "//flare/base/monitoring:monitoring_system",
        "//flare/base/thread:out_of_duty_callback",
        "//flare/base/thread:thread_local",
        "//flare/base/thread/thread_local:ref_counted",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_test(
    name = "monitoring_test",
    srcs = ["monitoring_test.cc"],
    # Heap checker is rather slow that we'd timeout if it's enabled.
    # heap_check = "",
    deps = [
        ":down_cast",
        ":monitoring",
        ":random",
        "//flare/base/monitoring:monitoring_system",
        "//flare/fiber",
        "//flare/init:override_flag",
        "//flare/testing:main",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_test(
    name = "monitoring_with_global_tags_test",
    srcs = ["monitoring_with_global_tags_test.cc"],
    deps = [
        ":down_cast",
        ":monitoring",
        ":random",
        "//flare/base/monitoring:monitoring_system",
        "//flare/fiber",
        "//flare/init:override_flag",
        "//flare/testing:main",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_test(
    name = "monitoring_benchmark",
    srcs = ["monitoring_benchmark.cc"],
    deps = [
        ":monitoring",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_library(
    name = "compression",
    srcs = ["compression.cc"],
    hdrs = ["compression.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/buffer:compression_output_stream",
        "//flare/base/compression",
        "//flare/base/compression:gzip",
        "//flare/base/compression:lz4",
        "//flare/base/compression:snappy",
        "//flare/base/compression:zstd",
    ],
)

cc_test(
    name = "compression_test",
    srcs = ["compression_test.cc"],
    deps = [
        ":compression",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "encoding",
    hdrs = ["encoding.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/encoding:base64",
        "//flare/base/encoding:hex",
        "//flare/base/encoding:percent",
    ],
)

cc_library(
    name = "expected",
    hdrs = ["expected.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base/internal:logging",
        "//flare/base/internal:meta",
    ],
)

cc_test(
    name = "expected_test",
    srcs = ["expected_test.cc"],
    deps = [
        ":expected",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "likely",
    hdrs = ["likely.h"],
    visibility = ["//visibility:public"],
)

cc_library(
    name = "dependency_registry",
    hdrs = ["dependency_registry.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":demangle",
        ":function",
        ":maybe_owning",
        ":never_destroyed",
        "//flare/base/internal:hash_map",
        "//flare/base/internal:lazy_init",
        "//flare/base/internal:logging",
    ],
)

cc_test(
    name = "dependency_registry_test",
    srcs = ["dependency_registry_test.cc"],
    deps = [
        ":dependency_registry",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "handle",
    hdrs = ["handle.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":logging",
    ],
)

cc_test(
    name = "handle_test",
    srcs = ["handle_test.cc"],
    deps = [
        ":handle",
        "@com_google_googletest//:gtest_main",
    ],
)
